<?php
namespace app\common\model;
use think\Validate;
use think\Db;
class Orders extends Base
{
	public static $pay_types = [
		1	=>'微信',
		2	=>'支付宝',
		3	=>'银联',
		4	=>'现金',
		5	=>'中行来聚财',
		6	=>'工商银行',
		7	=>'兴业银行',
		8	=>'淘宝',
		9 	=> '聚收宝',
		10 	=> '代收货款',
		11 	=> '富掌柜',
	];
	public static $expresses = ['顺丰','圆通','韵达','中通','德邦','EMS'];
	
	public function items()
	{
		return $this->hasMany('OrdersItem','order_id');
	}
	public function areaInfo()
	{
		return $this->belongsTo('Area','area_id');
	}
	public function acctInfo()
	{
		return $this->belongsTo('account','acct_id');
	}
	
	public function getRelativeList($where=[],$page=1,$page_size=10)
	{
		$total = $this->alias('o')->join('__ORDERS_ITEM__ oi','o.id=oi.order_id')
									->join('__GOODS__ g','g.id=oi.goods_id')
									->where($where)
									->count();
		$rows = $this->alias('o')->join('__ORDERS_ITEM__ oi','o.id=oi.order_id')
								->join('__GOODS__ g','g.id=oi.goods_id')
								->where($where)
								->field('o.*,oi.*,g.cate_id,brand_id,name,picture,color,in_price')
								->order('o.add_time desc')
								->page($page,$page_size)->select();

		return [
			'total'		=> $total,
			'rows'		=> $rows
		];
	}
	public function insert($info)
	{
        //订单号
        $order_no = date('ym0001');
		$row = $this->where('order_no','>=',$order_no)->order('order_no desc')->find();
		//return $order;exit;
		if ($row) {
			$order_no = $row['order_no'] + 1;
		}
		
		$info['order_no'] = $order_no;
		
		$res = $this->add($info, true);
		if ($res['code']) {
			$res['order_id'] = $this->where('order_no',$order_no)->value('id');
		}
		return $res;
	}
}